<!doctype html>
<html ng-app='app'>
<head>
    <meta charset='utf-8'>
    <title>欣乐土-登录系统</title>
    <script src='../plugin/AngularJs/angular.min.js'></script>
    <script src='../plugin/AngularJs/angular-route.js'></script>
    <script src='../plugin/AngularJs/angular-sanitize.min.js'></script>
    <script src="../plugin/Jquery/jquery-2.0.min.js"></script>
    <script src="../plugin/Bootstrap/js/bootstrap.min.js"></script>
    <script src="../plugin/Uploadify/jquery.uploadify.min.js"></script>
    <script src='../plugin/QrCode/qrcode.js'></script>

    <link rel='icon' type='image/png' href='../image/logo-icon.png'>
    <link rel="stylesheet" type="text/css" href="../plugin/FontAwesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="../plugin/Bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../plugin/Uploadify/uploadify.css">
    <link rel='stylesheet' type='text/css' href='../css/bootstrap.dev.css'>
    <style type="text/css">
        .header {
            background-color: #04c5b0;
            color: #FFF;
            text-align: center;
            padding: 30px 0px;
            font-size: 45px;
        }
        .box-qrcode {
            text-align: center;
            margin-top: 5%;
        }
        .box-qrcode > .qrcode{
            display: inline-block;
            width: 365px;
        }
    </style>
</head>
<body>
    <div class='header'>欣乐土园长后台登录</div>
    <div class='box-qrcode' ng-controller='Login' id='box-qrcode'>
        <div class='qrcode' id='qrcode'></div>
    </div>
</body>

<script type="text/javascript" src='./common/function.js'></script>
<script type="text/javascript" src='./common/config.js'></script>
<script type="text/javascript" src='./common/route.js'></script>
<script type="text/javascript" src='./common/service.js'></script>
<script type="text/javascript">
app.controller('Login', function($http, $scope, $interval, common){
    //变量声明
    $scope.flag     = '';

    //加载数据
    $scope.load_data    = function(){
        var url     = config.prefix_url + '/Wechat/getOauthData';
        $http.get(url).success(function(info){
            console.log(info);
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            //二维码生成
            var qrCode  = new QRCode(document.getElementById('qrcode'), {
                width   : 360,
                height  : 360
            });
            qrCode.makeCode(info.data.url);

            //flag获取
            $scope.flag     = info.data.flag;

            //验证驱动
            $scope.verify_register();
        })
    }

    //扫码验证
    $scope.verify_register  = function(){
        if(!$scope.flag){
            common.doAlert('Flag获取失败!');
            return false;
        }

        var url     = config.prefix_url + '/Wechat/verifyOauth?flag='+$scope.flag;
        var count   = 0;
        var requesting  = false;
        var interval    = $interval(function(){
            if(requesting){
                return true;
            }
            requesting  = true;
            $http.get(url).success(function(info){
                requesting  = false;
                count   = count + 1;

                if(info.code == -503 && count <= 60){
                    return true;
                }else{
                    //取消轮训任务
                    $interval.cancel(interval);

                    //结果处理
                    if(info.code != 1){
                        common.dealError(info);
                        return false;
                    }

                    //登录成功
                    S('user_info', info.data.info);
                    window.open('index.html', '_self');
                    return true;
                }
            })
        }, 2000);
    }

    $scope.load_data();
})
</script>
</html>
